package com.ebao.action.user;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.ebao.action.base.BaseAction;
import com.ebao.constants.UserType;
import com.ebao.po.user.TUserRegister;
import com.ebao.service.user.IUserRegisterService;
import com.ebao.util.PageResult;
import com.ebao.util.encrypt.MD5;

/**
 * @author snow
 * @date 2012-7-18
 */
public class UserRegisterAction extends BaseAction {

	private static final long serialVersionUID = -6799977600583837827L;

	@Resource
	private IUserRegisterService userRegisterService;
	
	private String id;
	private String info;	// json返回的信息
	
	/**
	 * 查看注册申请
	 */
	@Action(value = "list-register", results = {@Result(name = SUCCESS, type = "json",params = {
			"ignoreHierarchy", "false", "root", "userRegisters" }, location = "")})
	public String list(){
		String jpql = "select * from TUserRegister r where r.status is null";// 为null表示还未审核
		PageResult<TUserRegister> userRegisters = userRegisterService.getPageByJpqlAfterWhere(jpql, 1, 20);
		return SUCCESS;
	}
	
	/**
	 * 审核通过,邮件通知用户/医生
	 */
	@Action(value = "pass-register", results = {@Result(name = SUCCESS, type = "json", params = {"root", "info"})})
	public String pass(){
		TUserRegister userRegister = userRegisterService.getById(id);
		userRegister.setStatus(1);
		userRegisterService.update(userRegister);
		
		// 发送邮件
		String url = null;		// 激活账户URL
		String id = userRegister.getId() + "";
		String email = MD5.MD5Encode(userRegister.getEmail());
		if (userRegister.getUserType() == UserType.COMMON_USER) {// 用户
			
			
			url = "http://l27.0.0.1:8080/ebao/user/confirmRegister.html?encrypt=" + email + "&id="+ id;
			
			
		} else if (userRegister.getUserType() == UserType.DOCTOR) {// 医生
			
			url = "http://l27.0.0.1:8080/ebao/doctor/confirmRegister.html?encrypt=" + email + "&id="+ id;
			
		}
		
		info = "操作成功";
		
		return SUCCESS;
	}

	/**
	 * 审核不通过
	 */
	@Action(value = "not-pass-register", results = {@Result(name = SUCCESS, type = "json", params = {"root", "info"})})
	public String notPass(){
		TUserRegister userRegister = userRegisterService.getById(id);
		userRegister.setStatus(0);
		userRegisterService.update(userRegister);
		info = "操作成功";
		return SUCCESS;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	
}
